const dbConfig = require("../../../utils/dbConfig");

const getMoment = async (req, res) => {
  try {
    let sql = 'select * from moment'
    const data = await dbConfig.SySqlConnect(sql, [])
    res.status(200).send({
      code: 200,
      data: data,
      message: "success"
    })
  } catch (error) {
    console.log('/getMoment ERROR', error)
    res.status(500).send({
      code: 500,
      data: null,
      message: "服务器出错了"
    })
  }
}
// 获取评论
const getComments = async (req, res) => {
  let { momentID } = req.query
  let sql = 'select * from moment_comment where momentID=?'
  let sqlArr = [momentID]
  try {
    const data = await dbConfig.SySqlConnect(sql, sqlArr)
    res.status(200).send({
      code: 200,
      data: data,
      message: "success"
    })
  } catch (e) {
    res.status(500).send({
      code: 500,
      data: null,
      message: "服务器出错了"
    })
  }
}

// 屏蔽评论
const shieldMoment = async (req, res) => {
  try {
    let { momentID } = req.body
    let sql = 'update moment set status=-1 where momentID=?'
    let sqlArr = [momentID]
    const data = await dbConfig.SySqlConnect(sql, sqlArr)
    // 如果影响的行数为1，说明操作成功
    if (data.affectedRows === 1) {
      res.status(200).send({
        code: 200,
        data: null,
        message: "屏蔽成功"
      })
    } else {
      res.status(400).send({
        code: 400,
        data: null,
        message: "屏蔽失败"
      })
    }
  } catch (e) {
    res.status(500).send({
      code: 500,
      data: null,
      message: "服务器出错了"
    })
  }

}

const getMomentType = async (req, res) => {
  try {
    let sql = "select * from moment_type"
    const data = await dbConfig.SySqlConnect(sql, [])
    res.status(200).send({
      code: 200,
      data: data,
      message: "success"
    })
  } catch (error) {
    console.log('/getMomentType ERROR', error)
    res.status(500).send({
      code: 500,
      data: null,
      message: "服务器出错了"
    })
  }
}

const delMomentType = async (req, res) => {
  try {
    let { id } = req.body
    let sql = 'update moment_type set status = -1 where m_typeID=?'
    let sqlArr = [id]
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result.affectedRows === 1) {
      res.send({
        code: 200,
        message: 'success'
      })
    } else {
      res.send({
        code: 400,
        tip: 'failed'
      })
    }
  } catch (error) {
    res.status(500).send({
      code: 500,
      data: null,
      message: "服务器出错了"
    })
  }
}
createMomentType = async (req, res) => {
  try {
    let { name } = req.body
    let sql = 'insert into moment_type(name,create_time) values(?,?)'
    let sqlArr = [name, (new Date()).valueOf()]
    let result = await dbConfig.SySqlConnect(sql, sqlArr)
    if (result.affectedRows === 1) {
      res.send({
        code: 200,
        data: null,
        message: 'suceess'
      })
    } else {
      res.send({
        code: 400,
        data: null,
        message: 'failed'
      })
    }
  } catch (error) {
    res.status(500).send({
      code: 500,
      data: null,
      message: "服务器出错了"
    })
  }
}

module.exports = {
  getMoment,
  getComments,
  shieldMoment,
  getMomentType,
  delMomentType,
  createMomentType
}